/*
 * @Descripttion: 
 * @version: 
 * @Author: Ansel
 * @Date: 2020-04-05 15:06:31
 * @LastEditors: Ansel
 * @LastEditTime: 2020-04-05 15:30:20
 */
var eps = 0.00000000001;
function mySqrt(n){
    if(n < 0) return NaN;
    var mid;
    var last;
    var low = 0;
    var high = n;
    mid = (low + high)/2;
    do{
        if((mid * mid) > n)
            high = mid;
        else
            low = mid;
        last = mid;
        mid = (low + high) / 2; 
    }while(Math.abs(mid - last) > eps);
    return last;
}

console.log(mySqrt(2));
console.log(Math.sqrt(2))